fetch("assets/book_data.json").then(response => {
    if(response.ok){
        return response.json();
    }
    else {
        console.error(response.status);
    }
})
    .then(Books => {
        let Book;
        for (let i = 0; i < Books.length; i++) {
            let bookTitle;

            table = document.getElementById("table");
            tr = document.createElement("tr");
            table.appendChild(tr);

            td_1 = document.createElement("td");
            tr.appendChild(td_1);

            td_2 = document.createElement("td");
            img_2 = document.createElement("img");
            img_2.setAttribute("class","image")
            tr.appendChild(td_2);
            td_2.appendChild(img_2);

            td_3 = document.createElement("td");
            td_3.setAttribute("id","type"+i);
            a_3 = document.createElement("a");
            tr.appendChild(a_3);
            a_3.appendChild(td_3);


            td_4 = document.createElement("td");
            tr.appendChild(td_4);

            td_5 = document.createElement("td");
            tr.appendChild(td_5);

            td_6 = document.createElement("td");
            tr.appendChild(td_6);

            td_7 = document.createElement("td");
            tr.appendChild(td_7);

            td_8 = document.createElement("td");
            btn_8 = document.createElement("button");
            btn_8.setAttribute("id","deletebutton")
            tr.appendChild(td_8);
            td_8.appendChild(btn_8);

            Book = Books[i];

            td_1.innerHTML = i;
            img_2.src = Book.img_url;
            td_3.innerHTML = Book.title;

            bookTitle = document.getElementById("type"+i).textContent;

            a_3.href = Book.jd_link;
            td_4.innerHTML = Book.author;
            td_5.innerHTML = Book.publisher;
            td_6.innerHTML = Book.publish_date;
            td_7.innerHTML = Book.price;

            btn_8.setAttribute("data-bs-target","#modal-1");
            btn_8.setAttribute("data-bs-toggle","modal");
            btn_8.addEventListener("click",function () {
                document.getElementById("modal-body").innerHTML = "确定要删除编号为"+i+"的"+"《"+bookTitle+"》"+"吗？";
                console.log("正在删除编号为"+i+"的图书");
                document.getElementById("confirm-button").addEventListener("click",function () {
                    console.log("已删除编号为"+i+"的图书");
                })
            })

        }
    })
    .catch(error =>{
        console.error(error);
    })
